package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysDicCategory;
import com.example.base.system.domain.SysDicData;
import com.example.base.system.domain.vo.SysDicDataVo;
import com.example.base.system.mapper.SysDicDataMapper;
import com.example.base.system.service.SysDicCategoryService;
import com.example.base.system.service.SysDicDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiong
 * @since 2024-02-19
 */
@Service
@Transactional
@CacheConfig(cacheNames = "SysDicDataServiceImpl")
public class SysDicDataServiceImpl extends ServiceImpl<SysDicDataMapper, SysDicData> implements SysDicDataService {

    @Autowired
    private SysDicCategoryService sysDicCategoryService;

    @Cacheable(cacheNames = "sysdicdata",key = "#id")
    @Override
    public SysDicData getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "sysdicdata", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "sysdicdata", key = "#sysdicdata.id")
    @Override
    public boolean updateById(SysDicData sysdicdata) {
        return super.updateById(sysdicdata);
    }

    @CachePut(cacheNames = "sysdicdata", key = "#sysdicdata.id")
    public SysDicData saveSysDicData(SysDicData sysdicdata) throws GlobalExcetion {
        super.save(sysdicdata);
        return sysdicdata;
    }

    @CacheEvict(cacheNames = "sysdicdata", key = "#sysdicdata.id")
    public SysDicData updateSysDicDataById(SysDicData sysdicdata) throws GlobalExcetion{
        super.updateById(sysdicdata);
        return sysdicdata;
    }

    @Override
    public SysDicData saveOrUpdateSysDicData(SysDicDataVo vo) throws GlobalExcetion {
        SysDicData sysDicData=new SysDicData();
        BeanUtils.copyProperties(vo,sysDicData);
        SysDicCategory sysDicCategory= sysDicCategoryService.getById(vo.getDicCategoryId());
        if(sysDicCategory==null){
            throw new GlobalExcetion(1,"字典分类不存在");
        }
        if(vo.getId()!=null){
            SysDicData sysDicDataEntity=super.baseMapper.selectById(vo.getId());
            if(sysDicDataEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            if(vo.getDicKey()!=null){
                if(!vo.getDicKey().equals(sysDicDataEntity.getDicKey())){
                    SysDicData no=getSysDicCategoryByKey(vo.getDicKey());
                    if(no!=null){
                        throw new GlobalExcetion(1,"key已存在");
                    }
                }
            }
            sysDicData.setUpdatetime(new Date());
            super.baseMapper.updateById(sysDicData);
        }else {
            if(vo.getDicKey()!=null){
                SysDicData no=getSysDicCategoryByKey(vo.getDicKey());
                if(no!=null){
                    throw new GlobalExcetion(1,"key已存在");
                }
            }
            sysDicData.setUpdatetime(new Date());
            sysDicData.setCreatetime(new Date());
            sysDicData.setDeleted(0);
            super.baseMapper.insert(sysDicData);
        }
        return sysDicData;
    }

    @Override
    public SysDicData getSysDicCategoryByKey(String key) throws GlobalExcetion {
        LambdaQueryWrapper<SysDicData> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicData::getDicKey,key);
        SysDicData dicData=super.baseMapper.selectOne(lambdaQueryWrapper);
        return dicData;
    }

    @Override
    public IPage<SysDicData> page(Serializable categporyId, Integer page, Integer pageSize) {
        LambdaQueryWrapper<SysDicData> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicData::getDicCategoryId,categporyId);
        lambdaQueryWrapper.orderByDesc(SysDicData::getCreatetime);
        IPage<SysDicData> iPage=super.baseMapper.selectPage(new Page(page,pageSize),lambdaQueryWrapper);
        return iPage;
    }
}
